What is ml-matrix?
The ml-matrix npm package provides a comprehensive set of tools for matrix operations and linear algebra. It is designed to be used in machine learning and data science applications, offering a variety of functionalities such as matrix creation, manipulation, decomposition, and solving linear systems.
What are ml-matrix's main functionalities?
Matrix Creation
This feature allows you to create matrices from arrays. The code sample demonstrates how to create a 2x2 matrix and print it.
const { Matrix } = require('ml-matrix');
const A = new Matrix([[1, 2], [3, 4]]);
console.log(A.toString());
Matrix Operations
This feature supports various matrix operations such as addition, subtraction, multiplication, etc. The code sample shows how to add two matrices.
const { Matrix } = require('ml-matrix');
const A = new Matrix([[1, 2], [3, 4]]);
const B = new Matrix([[5, 6], [7, 8]]);
const C = A.add(B);
console.log(C.toString());
Matrix Decomposition
This feature provides methods for matrix decomposition like Singular Value Decomposition (SVD), LU decomposition, etc. The code sample demonstrates how to perform SVD on a matrix.
const { Matrix, SingularValueDecomposition } = require('ml-matrix');
const A = new Matrix([[1, 2], [3, 4]]);
const svd = new SingularValueDecomposition(A);
console.log(svd.diagonalMatrix.toString());
Solving Linear Systems
This feature allows solving linear systems of equations. The code sample shows how to solve the system Ax = b.
const { Matrix } = require('ml-matrix');
const A = new Matrix([[1, 2], [3, 4]]);
const b = [5, 6];
const x = A.solve(b);
console.log(x.toString());
Other packages similar to ml-matrix
mathjs
Math.js is an extensive math library for JavaScript and Node.js. It provides a wide range of mathematical functions and supports complex numbers, matrices, units, and more. Compared to ml-matrix, math.js offers a broader range of mathematical functionalities but may not be as specialized in matrix operations and linear algebra.
numeric
Numeric.js is a library for numerical computations in JavaScript. It provides functions for matrix operations, solving linear systems, and performing numerical integration. While it offers similar functionalities to ml-matrix, it is more focused on numerical methods and less on machine learning applications.
ndarray
Ndarray is a JavaScript library for multidimensional arrays. It provides efficient storage and manipulation of large datasets. While it can be used for matrix operations, it is more general-purpose and not specifically tailored for linear algebra like ml-matrix.
ml-matrix
Matrix manipulation and computation library.
Installation
$ npm install --save ml-matrix
Usage
As an ES module
import Matrix from 'ml-matrix';
const matrix = Matrix.ones(5, 5);
As a CommonJS module
const { Matrix } = require('ml-matrix');
const matrix = Matrix.ones(5, 5);
Examples
Standard operations
const { Matrix } = require('ml-matrix');
var A = new Matrix([[1, 1], [2, 2]]);
var B = new Matrix([[3, 3], [1, 1]]);
var C = new Matrix([[3, 3], [1, 1]]);
const addition = Matrix.add(A, B);
const substraction = Matrix.sub(A, B);
const multiplication = A.mmul(B);
const mulByNumber = Matrix.mul(A, 10);
const divByNumber = Matrix.div(A, 10);
const modulo = Matrix.mod(B, 2);
const maxMatrix = Matrix.max(A, B);
const minMatrix = Matrix.min(A, B);
C.add(A);
C.sub(A);
C.mul(10);
C.div(10);
C.mod(2);
var A = new Matrix([[1, 1], [-1, -1]]);
var expon = Matrix.exp(A);
var cosinus = Matrix.cos(A);
var absolute = Matrix.abs(A);
var numberRows = A.rows;
var numberCols = A.columns;
var firstValue = A.get(0, 0);
var numberElements = A.size;
var isRow = A.isRowVector();
var isColumn = A.isColumnVector();
var isSquare = A.isSquare();
var isSym = A.isSymmetric();
A.set(1, 0, 10);
var diag = A.diag();
var m = A.mean();
var product = A.prod();
var norm = A.norm();
var transpose = A.transpose();
var z = Matrix.zeros(3, 2);
var z = Matrix.ones(2, 3);
var z = Matrix.eye(3, 4);
Maths :
const {
Matrix,
inverse,
solve,
linearDependencies,
QrDecomposition,
LuDecomposition,
CholeskyDecomposition
} = require('ml-matrix');
var A = new Matrix([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
var inverseA = inverse(A);
var B = A.mmul(inverseA);
var A = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
var inverseA = inverse(A, (useSVD = true));
var B = A.mmul(inverseA);
var A = new Matrix([[1, 2], [3, 4], [5, 6]]);
var pseudoInverseA = A.pseudoInverse();
var B = A.mmul(pseudoInverseA).mmul(A);
var A = new Matrix([[3, 1], [4.25, 1], [5.5, 1], [8, 1]]);
var b = Matrix.columnVector([4.5, 4.25, 5.5, 5.5]);
var x = solve(A, b);
var error = Matrix.sub(b, A.mmul(x));
var A = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
var b = Matrix.columnVector([8, 20, 32]);
var x = solve(A, b, (useSVD = true));
var error = Matrix.sub(b, A.mmul(x));
var A = new Matrix([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
var QR = QrDecomposition(A);
var Q = QR.orthogonalMatrix;
var R = QR.upperTriangularMatrix;
var A = new Matrix([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
var LU = LuDecomposition(A);
var L = LU.lowerTriangularMatrix;
var U = LU.upperTriangularMatrix;
var P = LU.pivotPermutationVector;
var A = new Matrix([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
var cholesky = CholeskyDecomposition(A);
var L = cholesky.lowerTriangularMatrix;
var A = new Matrix([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
var e = EigenvalueDecomposition(A);
var real = e.realEigenvalues;
var imaginary = e.imaginaryEigenvalues;
var vectors = e.eigenvectorMatrix;
var A = new Matrix([
[2, 0, 0, 1],
[0, 1, 6, 0],
[0, 3, 0, 1],
[0, 0, 1, 0],
[0, 1, 2, 0]
]);
var dependencies = linearDependencies(A);
License
MIT